<div id="pages_category_product_product" class="scroll" style="height:100%;display:none;"><master :show-top="platform == 'Web' || platform == 'iOS' || platform == 'Android'" :title="title">
  <listpage ref="listpage"></listpage>
  <anyselect vkey="_id" title="选择商品" search-key="name" @change="submit" url="/api/model/product" ref="productAnySelect"></anyselect>
</master></div>
<script type="text/javascript">
(function (G) {
  G.vue("pages_category_product_product",{
  "usingComponents": {
    "master": "/components/Master/Master",
    "listpage": "/components/List/Page/Page",
    "anyselect": "/components/Input/AnySelect/AnySelect"
  },
  "enablePullDownRefresh": true
},{
    data: {
      categoryId:'',
      title:'',
      submiting: false,
      platform: G.platform
    },
    methods: {
      load: function (next) {
        this.selectComponent('#listpage').load(next);
      },
      refresh: function (next) {
        this.selectComponent('#listpage').refresh(next);
      },
      onPullDownRefresh: function (event) {
        this.refresh(function () {
          G.stopPullDownRefresh();
        })
      },
      onReachBottom: function (event) {
        this.load();
      },
      submit: function (event) {
        var _this = this, productIds = event.detail.value;
        if (productIds.length) {
          for (var i = 0; i < productIds.length; i++) productIds[i] = productIds[i]._id;
          _this.setData({ submiting: true });
          G.ajax('/category/product', function (ret) {
            G.alertx(ret.errorMsg || '操作成功!');
            if (!ret.errorMsg) _this.refresh();
            _this.setData({ submiting: false });
          }, {
              data: {
                categoryId: _this.getData('categoryId'),
                productIds: productIds
              },
              meth: 'POST'
            })
        }
      }
    },
    mounted: function (query) {
      var _this = this,modelName = 'category_product';
      var title = query.name + ' 产品列表';
      this.selectComponent('#listpage').init({
        getUrl: {
          url: '/category/product',
          data: {
            categoryId: query.id
          }
        },
        models: {
          name: {
            label: '商品名称',
            type:'input'
          },
          imgs: {
            label: '头图',
            type: 'image'
          }
        },
        searchKey: 'name',
        canBack:true,
        actions: {
          add: {
            action: function (event) {
              if (_this.getData('submiting')) return false;
              _this.selectComponent('#productAnySelect').open(event.event, {
                value: _this.selectComponent('#listpage').getRows(),
                contentStyle:G.platform == 'Web' ? 'right:16px;left:auto;' : ''
              })
            }
          },
          delet: {
            action: '/api/model/category_product?sql=' + JSON.stringify({real:true}),
            name: '删除'
          }
        }
      })
      this.load();
      this.setData({ title: title, categoryId: query.id });
      G.setNavigationBarTitle({
        title: title
      })
    }
  });
})(Y)
</script>